package db_models

import (
	"time"

	"cvevulner/models"

	"github.com/astaxie/beego/orm"
)

// UpdateYamlOriginMark Update origin data
func UpdateYamlOriginMark(ORM orm.Ormer) error {
	sqlString := "update cve_open_guss_yaml set mark_bit = 2 where mark_bit = 1"
	_, err := ORM.Raw(sqlString).Exec()
	return err
}

// SelectYamlOriginData Query origin data
func SelectYamlOriginData(packageName, version string, ORM orm.Ormer) (models.OpenGussYaml, error) {
	var openGussYaml models.OpenGussYaml
	sqlString := "SELECT id FROM cve_open_guss_yaml WHERE package_name = ? and version = ?"
	err := ORM.Raw(sqlString, packageName, version).QueryRow(&openGussYaml)
	return openGussYaml, err
}

// InsertYamlOriginData insert origin data
func InsertYamlOriginData(openGussYaml *models.OpenGussYaml, ORM orm.Ormer) error {
	sqlString := "insert into cve_open_guss_yaml(package_name,version,origin_url, " +
		"status, cpe_name,create_time,update_time, mark_bit, repo_name, warehouse) values(?,?,?,?,?,?,?,?,?,?)"
	_, err := ORM.Raw(sqlString, openGussYaml.PackageName, openGussYaml.Version,
		openGussYaml.OriginUrl, openGussYaml.Status, openGussYaml.CpeName, openGussYaml.CreateTime,
		openGussYaml.UpdateTime, openGussYaml.MarkBit, openGussYaml.Repo, openGussYaml.Warehouse).Exec()
	return err
}

// UpdateYamlOriginData Update origin data
func UpdateYamlOriginData(openGussYaml *models.OpenGussYaml, ORM orm.Ormer) (int64, error) {
	id, err := ORM.Update(openGussYaml, "OriginUrl", "Status", "CpeName", "UpdateTime", "Repo", "MarkBit", "Warehouse")
	return id, err
}

// SelectYamlData Query data
func SelectYamlData(packageName, version string, ORM orm.Ormer) ([]models.GitOpenEuler, int64, error) {
	var gitOpenEulerList []models.GitOpenEuler
	sqlString := "SELECT package_id, git_id FROM cve_git_open_euler WHERE " +
		"package_name = ? and version = ? order by package_id desc"
	rowsAffected, err := ORM.Raw(sqlString, packageName, version).QueryRows(&gitOpenEulerList)
	return gitOpenEulerList, rowsAffected, err
}

// SelectYamlLastData Query the last data
func SelectYamlLastData(ORM orm.Ormer) (models.GitOpenEuler, error) {
	var gitOpenEuler models.GitOpenEuler
	sqlString := "SELECT package_id FROM cve_git_open_euler order by git_id desc"
	err := ORM.Raw(sqlString).QueryRow(&gitOpenEuler)
	return gitOpenEuler, err
}

// InsertYamlData insert data
func InsertYamlData(gitOpenEuler *models.GitOpenEuler, ORM orm.Ormer) (int64, error) {
	sqlString := "insert into cve_git_open_euler(package_id,package_name,version,origin_url," +
		"create_time,update_time,cpe_packname,status) values(?,?,?,?,?,?,?,?)"
	result, err := ORM.Raw(sqlString, gitOpenEuler.PackageId, gitOpenEuler.PackageName,
		gitOpenEuler.Version, gitOpenEuler.OriginUrl, gitOpenEuler.CreateTime, gitOpenEuler.UpdateTime,
		gitOpenEuler.CpePackName, gitOpenEuler.Status).Exec()
	if err != nil {
		return -1, err
	}

	id, _ := result.LastInsertId()

	return id, err
}

// InsertYamlDetailData insert data details
func InsertYamlDetailData(gitPackageInfo *models.GitPackageInfo, ORM orm.Ormer) error {
	sqlString := "insert into cve_git_package_info(git_id,package_name,version," +
		"origin_url,create_time,update_time,decription,status) values(?,?,?,?,?,?,?,?)"
	_, err := ORM.Raw(sqlString, gitPackageInfo.GitId, gitPackageInfo.PackageName,
		gitPackageInfo.Version, gitPackageInfo.OriginUrl, gitPackageInfo.CreateTime,
		gitPackageInfo.UpdateTime, gitPackageInfo.Decription, gitPackageInfo.Status).Exec()
	return err
}

// DeleteYamlOpenEulerDetailData delete origin data
func DeleteYamlOpenEulerDetailData(gitId int64, ORM orm.Ormer) error {
	sqlString := "delete from cve_git_package_info where git_id = ?"
	_, err := ORM.Raw(sqlString, gitId).Exec()
	return err
}

// DeleteYamlOpenEulerData delete origin data
func DeleteYamlOpenEulerData(gitId int64, ORM orm.Ormer) error {
	sqlString := "delete from cve_git_open_euler where git_id = ?"
	_, err := ORM.Raw(sqlString, gitId).Exec()
	return err
}

// UpdateYamlData update data
func UpdateYamlData(gitOpenEuler *models.GitOpenEuler, ORM orm.Ormer) error {
	sqlString := "update cve_git_open_euler set origin_url = ?,update_time = ?," +
		"cpe_packname = ?,status=? where package_id = ? and package_name = ? and version = ?"
	_, err := ORM.Raw(sqlString, gitOpenEuler.OriginUrl, gitOpenEuler.UpdateTime, gitOpenEuler.CpePackName,
		gitOpenEuler.Status, gitOpenEuler.PackageId, gitOpenEuler.PackageName, gitOpenEuler.Version).Exec()
	return err
}

// UpdateYamlDetailData Update data details
func UpdateYamlDetailData(gitPackageInfo *models.GitPackageInfo, ORM orm.Ormer) error {
	sqlString := "update cve_git_package_info set package_name = ?,version = ?," +
		"origin_url = ?, update_time = ?,status=? where git_id = ?"
	_, err := ORM.Raw(sqlString, gitPackageInfo.PackageName, gitPackageInfo.Version,
		gitPackageInfo.OriginUrl, gitPackageInfo.UpdateTime, gitPackageInfo.Status, gitPackageInfo.GitId).Exec()
	return err
}

// DeleteYamlOriginMark delete origin data
func DeleteYamlOriginMark(ORM orm.Ormer) error {
	sqlString := "delete from cve_open_guss_yaml where mark_bit = 2"
	_, err := ORM.Raw(sqlString).Exec()
	return err
}

// SelectOpenEulerYamlData Query data
func SelectOpenEulerYamlData(ORM orm.Ormer) ([]models.GitOpenEuler, int64, error) {
	var gitOpenEulerList []models.GitOpenEuler
	sqlString := "SELECT package_id, git_id,package_name,version FROM cve_git_open_euler WHERE package_id >= ?"
	rowsAffected, err := ORM.Raw(sqlString, 10000000).QueryRows(&gitOpenEulerList)
	return gitOpenEulerList, rowsAffected, err
}

// SelectGaussYamlOriginData Query origin data
func SelectGaussYamlOriginData(packageName, version string, ORM orm.Ormer) (models.OpenGussYaml, error) {
	var openGussYaml models.OpenGussYaml
	sqlString := "SELECT id FROM cve_open_guss_yaml WHERE package_name = ? and version = ? order by id limit 1"
	err := ORM.Raw(sqlString, packageName, version).QueryRow(&openGussYaml)
	return openGussYaml, err
}

// SelectMindSporeYamlOriginData Query origin data
func SelectMindSporeYamlOriginData(packageName, version string, ORM orm.Ormer) (models.MindSporeYaml, error) {
	var mindSporeYaml models.MindSporeYaml
	sqlString := "SELECT id FROM cve_mind_spore_yaml WHERE package_name = ? and version = ? order by id limit 1"
	err := ORM.Raw(sqlString, packageName, version).QueryRow(&mindSporeYaml)
	return mindSporeYaml, err
}

// SelectOpenLookengYamlOriginData Query origin data
func SelectOpenLookengYamlOriginData(packageName, version string, ORM orm.Ormer) (models.OpenLookengYaml, error) {
	var openLookengYaml models.OpenLookengYaml
	sqlString := "SELECT id FROM cve_open_lookeng_yaml WHERE package_name = ? and version = ? order by id limit 1"
	err := ORM.Raw(sqlString, packageName, version).QueryRow(&openLookengYaml)
	return openLookengYaml, err
}

func GetOneGitRepoGroups(sigName string, ORM orm.Ormer) (models.GitRepoGroups, error) {
	var gitRepsGroups models.GitRepoGroups
	sqlString := "select * from cve_git_repo_groups where group_name = ?"
	err := ORM.Raw(sqlString, sigName).QueryRow(&gitRepsGroups)
	return gitRepsGroups, err
}

func InsertOneGitRepoGroups(gitRepoGroups *models.GitRepoGroups, ORM orm.Ormer) error {
	sqlString := "insert into cve_git_repo_groups (group_name) values (?)"
	_, err := ORM.Raw(sqlString, gitRepoGroups.GroupName).Exec()
	return err
}

func DeleteGitRepoMember(groupId int64, ORM orm.Ormer) error {
	sqlString := "delete from cve_gite_repo_member where group_id = ?"
	_, err := ORM.Raw(sqlString, groupId).Exec()
	return err
}

func InsertOneGiteRepoMember(giteRepoMember *models.GiteRepoMember, ORM orm.Ormer) error {
	sqlString := "insert into cve_gite_repo_member (group_id," +
		"member_name,member_type,create_time) values (?, ?, ?, ?)"
	_, err := ORM.Raw(sqlString, giteRepoMember.GroupId, giteRepoMember.MemberName,
		giteRepoMember.MemberType, giteRepoMember.CreateTime).Exec()
	return err
}

func GetCveScoreListByBeforeDate(beforeDate string, ORM orm.Ormer) ([]models.Score, int64, error) {
	var scoreList []models.Score
	sqlString := "select cve_id,cve_num from cve_score where " +
		"nvd_score = 0 and update_time >= ? order by update_time desc"
	rowsAffected, err := ORM.Raw(sqlString, beforeDate).QueryRows(&scoreList)
	return scoreList, rowsAffected, err
}

func GetCveVulnCenterList(cveDesc, repairTime, updateTime string, ORM orm.Ormer) ([]models.VulnCenter, int64, error) {
	var vulnCenterList []models.VulnCenter
	sqlString := "select cve_id,cve_num from cve_vuln_center where " +
		"(cve_desc = ? or repair_time = ?) and update_time >= ? order by cve_id desc"
	rowsAffected, err := ORM.Raw(sqlString, cveDesc, repairTime, updateTime).QueryRows(&vulnCenterList)
	return vulnCenterList, rowsAffected, err
}

func GetCveIssueTemplate(nvdScore, nvdVector, cveBrief, updateTime string, ORM orm.Ormer) ([]models.IssueTemplate, int64, error) {
	var issueTemplateList []models.IssueTemplate
	sqlString := "select cve_id,cve_num from cve_issue_template where " +
		"(nvd_score = ? or nvd_vector = ? or cve_brief = ? or status in (1,2)) and update_time >= ? order by cve_id desc"
	rowsAffected, err := ORM.Raw(sqlString, nvdScore, nvdVector, cveBrief, updateTime).QueryRows(&issueTemplateList)
	return issueTemplateList, rowsAffected, err
}

func GetOneVulnCenter(cveId, cveNum string, ORM orm.Ormer) (models.VulnCenter, error) {
	var VulnCenter models.VulnCenter
	sqlString := "select cve_num, pack_name, cve_version, " +
		"cve_desc,repair_time,cve_status,cve_id from cve_vuln_center where cve_id = ? and cve_num = ? order by cve_id desc"
	err := ORM.Raw(sqlString, cveId, cveNum).QueryRow(&VulnCenter)
	return VulnCenter, err
}

func UpdateVulnCenterTypeOne(vulnCenter models.VulnCenter, ORM orm.Ormer) error {
	sqlString := "update cve_vuln_center set repair_time = ?,update_time=?, cve_status=? where cve_id=?"
	_, err := ORM.Raw(sqlString, vulnCenter.RepairTime, vulnCenter.UpdateTime, vulnCenter.Status, vulnCenter.CveId).Exec()
	return err
}

func UpdateVulnCenterTypeTwo(vulnCenter models.VulnCenter, ORM orm.Ormer) error {
	sqlString := "update cve_vuln_center set cve_desc = ?,update_time=?, cve_status=? where cve_id=?"
	_, err := ORM.Raw(sqlString, vulnCenter.Description, vulnCenter.UpdateTime, vulnCenter.Status, vulnCenter.CveId).Exec()
	return err
}

func GetCveScoreByCveIdAndCveNum(cveId int64, cveNum string, ORM orm.Ormer) (models.Score, error) {
	var score models.Score
	sqlString := "select id,nvd_score from cve_score where cve_id = ? and cve_num = ?"
	err := ORM.Raw(sqlString, cveId, cveNum).QueryRow(&score)
	return score, err
}

func UpdateCveScore(score models.Score, ORM orm.Ormer) error {
	sqlString := "update cve_score set nvd_score=?, n_vector_value=?, n_attack_vector=?, " +
		"n_access_vector=?, n_attack_complexity=?, n_access_complexity=?, n_privilege_required=?, " +
		"n_user_interaction=?, n_scope=?, n_confidentiality=?, n_integrity=?, n_availability=?, " +
		"n_authentication=?, update_time=?,score_type=? where id = ?"
	_, err := ORM.Raw(sqlString, score.NVDScore, score.NvectorVule, score.NattackVector,
		score.NaccessVector, score.NattackComplexity, score.NaccessComplexity, score.NprivilegeRequired,
		score.NuserInteraction, score.Nscope, score.Nconfidentiality, score.Nintegrity, score.Navailability,
		score.Nauthentication, score.UpdateTime, score.ScoreType, score.Id).Exec()
	return err
}

func GetIssueTemplateTypeOne(issueTemplate models.IssueTemplate, ORM orm.Ormer) (models.IssueTemplate, error) {
	var issueTemplateResp models.IssueTemplate
	sqlString := "select template_id,nvd_score,nvd_vector,cve_brief from cve_issue_template where cve_id = ? and cve_num = ?"
	err := ORM.Raw(sqlString, issueTemplate.CveId, issueTemplate.CveNum).QueryRow(&issueTemplateResp)
	return issueTemplateResp, err
}

func UpdateCveIssueTemplateTypeOne(issueTemplate models.IssueTemplate, ORM orm.Ormer) error {
	sqlString := "update cve_issue_template set nvd_score = ?, update_time=? where template_id = ?"
	_, err := ORM.Raw(sqlString, issueTemplate.NVDScore, issueTemplate.UpdateTime, issueTemplate.TemplateId).Exec()
	return err
}

func UpdateCveIssueTemplateTypeTwo(issueTemplate models.IssueTemplate, ORM orm.Ormer) error {
	sqlString := "update cve_issue_template set nvd_vector = ?, update_time=? where template_id = ? "
	_, err := ORM.Raw(sqlString, issueTemplate.NVDVector, issueTemplate.UpdateTime, issueTemplate.TemplateId).Exec()
	return err
}

func UpdateCveIssueTemplateTypeThree(issueTemplate models.IssueTemplate, ORM orm.Ormer) error {
	sqlString := "update cve_issue_template set cve_brief  = ?, update_time=? where template_id = ? "
	_, err := ORM.Raw(sqlString, issueTemplate.CveBrief, issueTemplate.UpdateTime, issueTemplate.TemplateId).Exec()
	return err
}

func UpdateVulnCenter(vulnCenter models.VulnCenter, ORM orm.Ormer) error {
	sqlString := "update cve_vuln_center set update_time=?, cve_status=? where cve_id=?"
	_, err := ORM.Raw(sqlString, vulnCenter.UpdateTime, vulnCenter.Status, vulnCenter.CveId).Exec()
	return err
}

func GetSpceError(cveNum, cveOwner, packName string, ORM orm.Ormer) (models.SpecError, error) {
	var spceError models.SpecError
	sqlString := "select * from cve_spec_error where cve_num = ? and cve_owner = ? and pack_name = ?"
	err := ORM.Raw(sqlString, cveNum, cveOwner, packName).QueryRow(&spceError)
	return spceError, err
}

func UpdateCveOriginExcel(originExcel models.OriginExcel, ORM orm.Ormer) error {
	sqlString := "update cve_origin_excel set cve_desc = ?, cve_status = ? where " +
		"cve_num= ? and pack_name = ? and cve_version = ?"
	_, err := ORM.Raw(sqlString, originExcel.CveDesc, originExcel.CveStatus,
		originExcel.CveNum, originExcel.PackName, originExcel.CveVersion).Exec()
	return err
}

// UpdateMindYamlOriginMark UpdateYamlOriginMarkLookeng Update origin data
func UpdateMindYamlOriginMark(ORM orm.Ormer) error {
	sqlString := "update cve_mind_spore_yaml set mark_bit = 2 where mark_bit = 1"
	_, err := ORM.Raw(sqlString).Exec()
	return err
}

// UpdateOpenlookengYamlOriginMark UpdateYamlOriginMarkLookeng Update origin data
func UpdateOpenlookengYamlOriginMark(ORM orm.Ormer) error {
	sqlString := "update cve_open_lookeng_yaml set mark_bit = 2 where mark_bit = 1"
	_, err := ORM.Raw(sqlString).Exec()
	return err
}

// SelectMindYamlOriginData Query origin data
func SelectMindYamlOriginData(packageName, version, repokey string, ORM orm.Ormer) (models.OpenGussYaml, error) {
	var openGussYaml models.OpenGussYaml
	sqlString := "SELECT id FROM cve_mind_spore_yaml WHERE package_name = ? and version = ? and repo_name = ?"
	err := ORM.Raw(sqlString, packageName, version, repokey).QueryRow(&openGussYaml)
	return openGussYaml, err
}

// SelectOpenlookengYamlOriginData Query origin data
func SelectOpenlookengYamlOriginData(packageName, version, repokey string, ORM orm.Ormer) (models.OpenLookengYaml, error) {
	var openLookengYaml models.OpenLookengYaml
	sqlString := "SELECT id FROM cve_open_lookeng_yaml WHERE package_name = ? and version = ? and repo_name = ?"
	err := ORM.Raw(sqlString, packageName, version, repokey).QueryRow(&openLookengYaml)
	return openLookengYaml, err
}

// InsertMindYamlOriginData insert origin data
func InsertMindYamlOriginData(mindSporeYaml *models.MindSporeYaml, ORM orm.Ormer) error {
	sqlString := "insert into cve_mind_spore_yaml(package_name,version,origin_url, status, " +
		"cpe_name,create_time,update_time, mark_bit, repo_name, owner, milestone) values(?,?,?,?,?,?,?,?,?,?,?)"
	_, err := ORM.Raw(sqlString, mindSporeYaml.PackageName, mindSporeYaml.Version,
		mindSporeYaml.OriginUrl, mindSporeYaml.Status, mindSporeYaml.CpeName, mindSporeYaml.CreateTime,
		mindSporeYaml.UpdateTime, mindSporeYaml.MarkBit, mindSporeYaml.Repo, mindSporeYaml.Owner, mindSporeYaml.Milestone).Exec()
	return err
}

// InsertOpenlookengYamlOriginData insert origin data
func InsertOpenlookengYamlOriginData(openLookengYaml *models.OpenLookengYaml, ORM orm.Ormer) error {
	sqlString := "insert into cve_open_lookeng_yaml(package_name,version,origin_url, " +
		"status, cpe_name,create_time,update_time, mark_bit, repo_name) values(?,?,?,?,?,?,?,?,?)"
	_, err := ORM.Raw(sqlString, openLookengYaml.PackageName, openLookengYaml.Version,
		openLookengYaml.OriginUrl, openLookengYaml.Status, openLookengYaml.CpeName,
		openLookengYaml.CreateTime, openLookengYaml.UpdateTime, openLookengYaml.MarkBit, openLookengYaml.Repo).Exec()
	return err
}

// UpdateMindYamlOriginData Update origin data
func UpdateMindYamlOriginData(mindSporeYaml *models.MindSporeYaml, ORM orm.Ormer) (int64, error) {
	id, err := ORM.Update(mindSporeYaml, "OriginUrl", "Status", "CpeName", "UpdateTime", "MarkBit", "Owner", "Milestone")
	return id, err
}

// UpdateMindYamlOriginData Update origin data
func UpdateOpenlookengYamlOriginData(openLookengYaml *models.OpenLookengYaml, ORM orm.Ormer) (int64, error) {
	id, err := ORM.Update(openLookengYaml, "OriginUrl", "Status", "CpeName", "UpdateTime", "MarkBit")
	return id, err
}

// DeleteMindYamlOriginMark delete origin data
func DeleteMindYamlOriginMark(ORM orm.Ormer) error {
	sqlString := "delete from cve_mind_spore_yaml where mark_bit = 2"
	_, err := ORM.Raw(sqlString).Exec()
	return err
}

func SelectMindSporeSecurityReviewer(nameSpace string, ORM orm.Ormer) (models.Reviewer, error) {
	var mindSporeSecurityReviewer models.Reviewer
	sqlString := "SELECT id FROM cve_reviewer WHERE name_space = ? and organizate_id = 3"
	err := ORM.Raw(sqlString, nameSpace).QueryRow(&mindSporeSecurityReviewer)
	return mindSporeSecurityReviewer, err
}

// InsertMindYamlOriginData insert origin data
func InsertMindSporeSecurityReviewer(reviewer *models.Reviewer, ORM orm.Ormer) error {
	sqlString := "insert into cve_reviewer(name_space,status,organizate_id) values(?,?,?)"
	_, err := ORM.Raw(sqlString, reviewer.NameSpace, reviewer.Status, reviewer.OrganizationID).Exec()
	return err
}

// DeleteMindYamlOriginMark delete origin data
func DeleteOpenlookengYamlOriginMark(ORM orm.Ormer) error {
	sqlString := "delete from cve_open_lookeng_yaml where mark_bit = 2"
	_, err := ORM.Raw(sqlString).Exec()
	return err
}

func SelectCveFileHashByFileName(fileName string, ORM orm.Ormer) ([]interface{}, int64, error) {
	var list orm.ParamsList
	sqlString := "select file_hash from cve_file_hash where file_name = ?"
	num, err := ORM.Raw(sqlString, fileName).ValuesFlat(&list)
	return list, num, err
}

func InsertCveFileHash(fileName, fileHash string, ORM orm.Ormer) error {
	sqlString := "insert into cve_file_hash (file_name, file_hash) values (?, ?)"
	_, err := ORM.Raw(sqlString, fileName, fileHash).Exec()
	return err
}

func InsertSpecIssueAssigness(specIssueAssigness *models.SpecIssueAssigness, ORM orm.Ormer) error {
	sqlString := "insert ignore into cve_spec_issue_assigness " +
		"(package_name, issue_assignee, status, create_time) values (?, ?, ?, ?)"
	_, err := ORM.Raw(sqlString, specIssueAssigness.PackageName, specIssueAssigness.Assignee,
		specIssueAssigness.Status, specIssueAssigness.CreateTime).Exec()
	return err
}

func SelectIssueRepoWhiteListByPackageName(packageName string, ORM orm.Ormer) (models.IssueRepoWhitelist, error) {
	var issueRepoWhiteList models.IssueRepoWhitelist
	sqlString := "select * from cve_issue_repo_whitelist where package_name = ?"
	err := ORM.Raw(sqlString, packageName).QueryRow(&issueRepoWhiteList)
	return issueRepoWhiteList, err
}

func InsertIssueRepoWhiteList(issueRepoWhiteList *models.IssueRepoWhitelist, ORM orm.Ormer) error {
	sqlString := "insert into cve_issue_repo_whitelist (package_name, version, " +
		"status, branchs, create_time, update_time, delete_time) values (?, ?, ?, ?, ?, ?, ?)"
	_, err := ORM.Raw(sqlString, issueRepoWhiteList.PackageName, issueRepoWhiteList.Version,
		issueRepoWhiteList.Status, issueRepoWhiteList.Branchs, issueRepoWhiteList.CreateTime,
		issueRepoWhiteList.UpdateTime, issueRepoWhiteList.DeleteTime).Exec()
	return err
}

func UpdateIssueRepoWhiteList(issueRepoWhiteList models.IssueRepoWhitelist, ORM orm.Ormer) error {
	sqlString := "update cve_issue_repo_whitelist set status = ?, " +
		"branchs = ?, update_time = ? where package_name = ? and version = ?"
	_, err := ORM.Raw(sqlString, issueRepoWhiteList.Status, issueRepoWhiteList.Branchs,
		issueRepoWhiteList.UpdateTime, issueRepoWhiteList.PackageName, issueRepoWhiteList.Version).Exec()
	return err
}

func GetResults(status string, ORM orm.Ormer) ([]models.IssueCreateRecord, int64, error) {
	lastMonthDate := time.Now().AddDate(0, -1, 0).Format("2006-01-02")
	var issueCreateRecordList []models.IssueCreateRecord
	sqlString := "select * from cve_issue_create_record where status = ? and create_time > ?"
	rowsAffected, err := ORM.Raw(sqlString, status, lastMonthDate).QueryRows(&issueCreateRecordList)
	return issueCreateRecordList, rowsAffected, err
}

func UpdateStatue(status string, ORM orm.Ormer) error {
	sqlString := "update cve_issue_create_record set status = ?,update_time = ? where status = ?"
	_, err := ORM.Raw(sqlString, 4, time.Now().Format("2006-01-02 15:04:05"), status).Exec()
	return err
}

func GetCveEmailListByEmailType(emailType string, ORM orm.Ormer) ([]models.EmailList, int64, error) {
	var emailList []models.EmailList
	sqlString := "select email_name from cve_email_list where email_type=?"
	rowsAffected, err := ORM.Raw(sqlString, emailType).QueryRows(&emailList)
	return emailList, rowsAffected, err
}

func SelectCvePackAgeCpeByPackName(packName string, ORM orm.Ormer) (models.PackageCpe, error) {
	var packageCpe models.PackageCpe
	sqlString := "select * from cve_package_cpe where packname = ?"
	err := ORM.Raw(sqlString, packName).QueryRow(&packageCpe)
	return packageCpe, err
}

func InsertCvePackAgeCpe(packageCpe *models.PackageCpe, ORM orm.Ormer) error {
	sqlString := "insert into cve_package_cpe (packname,cpe_packname,create_time) values (?,?,?)"
	_, err := ORM.Raw(sqlString, packageCpe.PackName, packageCpe.CpePackName, packageCpe.CreateTime).Exec()
	return err
}

func UpdateCvePackAgeCpe(packageCpe models.PackageCpe, ORM orm.Ormer) error {
	sqlString := "update cve_package_cpe set cpe_packname=?, create_time=? where packname=?"
	_, err := ORM.Raw(sqlString, packageCpe.CpePackName, packageCpe.CreateTime, packageCpe.PackName).Exec()
	return err
}

func GetCveOriginExcel(ORM orm.Ormer) ([]models.OriginExcel, int64, error) {
	lastMonthDate := time.Now().AddDate(0, -1, 0).Format("2006-01-02")
	var originExcel []models.OriginExcel
	sqlString := "select * from cve_origin_excel where " +
		"(cve_status = 3 or cve_status=4) and is_export = 1 and create_time > ?"
	rowsAffected, err := ORM.Raw(sqlString, lastMonthDate).QueryRows(&originExcel)
	return originExcel, rowsAffected, err
}

func UpdateStatusTypeTwo(ORM orm.Ormer) error {
	sqlString := "update cve_origin_excel set is_export=2 where cve_status=3 or cve_status=4"
	_, err := ORM.Raw(sqlString).Exec()
	return err
}

func GetCveSpecError(cveNum string, ORM orm.Ormer) (models.SpecError, error) {
	var specError models.SpecError
	sqlString := "SELECT * FROM cve_spec_error where cve_num = ?"
	err := ORM.Raw(sqlString, cveNum).QueryRow(&specError)
	return specError, err
}

func InsertCveSpecError(specError *models.SpecError, ORM orm.Ormer) (int64, error) {
	return ORM.Insert(specError)
}

func UpdateCveSpecError(specError models.SpecError, ORM orm.Ormer) error {
	sqlString := "update cve_spec_error set cve_desc = ?, cve_owner = ?, " +
		"cve_status = ?, pack_name = ?, update_time = ?  where cve_num = ?"
	_, err := ORM.Raw(sqlString, specError.Description, specError.Owner,
		specError.Status, specError.PackName, specError.UpdateTime, specError.CveNum).Exec()
	return err
}

func GetCveOriginExcelTypeTwo(cveNum, packName, cveVersion string, ORM orm.Ormer) (models.OriginExcel, error) {
	var originExcel models.OriginExcel
	sqlString := "select * from cve_origin_excel where cve_num= ? and pack_name = ? and cve_version = ?"
	err := ORM.Raw(sqlString, cveNum, packName, cveVersion).QueryRow(&originExcel)
	return originExcel, err
}

func UpdateCveOriginExcelTypeThree(originExcel models.OriginExcel, ORM orm.Ormer) error {
	sqlString := "update cve_origin_excel set nvd_score=?, cve_level=?, cve_desc=?, " +
		"repair_time=?, vector_value=?, attack_vector=?, access_vector=?, attack_complexity=?, " +
		"access_complexity=?, privilege_required=?, user_interaction=?, scope=?, " +
		"confidentiality=?, integrity=?, availability=?, authentication=?, cve_status=?, " +
		"update_time=? where cve_num=? and pack_name = ? and cve_version = ?"
	_, err := ORM.Raw(sqlString, originExcel.NVDScore, originExcel.CveLevel,
		originExcel.CveDesc, originExcel.RepairTime, originExcel.NVDVector, originExcel.AttackVector,
		originExcel.AccessVector, originExcel.AttackComplexity, originExcel.AccessComplexity,
		originExcel.PrivilegeRequired, originExcel.UserInteraction, originExcel.Scope,
		originExcel.Confidentiality, originExcel.Integrity, originExcel.Availability, originExcel.Authentication,
		originExcel.CveStatus, originExcel.UpdateTime, originExcel.CveNum, originExcel.PackName, originExcel.CveVersion).Exec()
	return err
}

func UpdateCveOriginExcelTypeFour(originExcel models.OriginExcel, ORM orm.Ormer) error {
	sqlString := "update cve_origin_excel set nvd_score=?, cve_level=?, cve_desc=?, repair_time=?, " +
		"vector_value=?, attack_vector=?, access_vector=?, attack_complexity=?, access_complexity=?, " +
		"privilege_required=?, user_interaction=?, scope=?, confidentiality=?, integrity=?, availability=?, " +
		"authentication=?, cve_status=?, update_time=?, score_type=? where cve_num=? and pack_name = ? and cve_version = ?"
	_, err := ORM.Raw(sqlString, originExcel.NVDScore, originExcel.CveLevel, originExcel.CveDesc,
		originExcel.RepairTime, originExcel.NVDVector, originExcel.AttackVector, originExcel.AccessVector,
		originExcel.AttackComplexity, originExcel.AccessComplexity, originExcel.PrivilegeRequired,
		originExcel.UserInteraction, originExcel.Scope, originExcel.Confidentiality, originExcel.Integrity,
		originExcel.Availability, originExcel.Authentication, originExcel.CveStatus, originExcel.UpdateTime,
		originExcel.ScoreType, originExcel.CveNum, originExcel.PackName, originExcel.CveVersion).Exec()
	return err
}

func UpdateCveOriginExcelTypeFive(originExcel models.OriginExcel, ORM orm.Ormer) error {
	sqlString := "update cve_origin_excel set cve_desc = ?, cve_status = ? where cve_num = ?"
	_, err := ORM.Raw(sqlString, originExcel.CveDesc, originExcel.CveStatus, originExcel.CveNum).Exec()
	return err
}

func InsertCveOriginExcel(originExcel *models.OriginExcel, ORM orm.Ormer) error {
	sqlString := "INSERT INTO cve_origin_excel (cve_num, cve_url, cve_version, pack_name, score_type, " +
		"nvd_score, cve_level, cve_desc, repair_time, vector_value, attack_vector, access_vector, " +
		"attack_complexity, access_complexity, privilege_required, user_interaction, scope, confidentiality, " +
		"integrity, availability, authentication, cve_status, create_time, update_time) " +
		"VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"
	_, err := ORM.Raw(sqlString, originExcel.CveNum, originExcel.CveUrl, originExcel.CveVersion,
		originExcel.PackName, originExcel.ScoreType, originExcel.NVDScore, originExcel.CveLevel,
		originExcel.CveDesc, originExcel.RepairTime, originExcel.NVDVector, originExcel.AttackVector,
		originExcel.AccessVector, originExcel.AttackComplexity, originExcel.AccessComplexity,
		originExcel.PrivilegeRequired, originExcel.UserInteraction, originExcel.Scope, originExcel.Confidentiality,
		originExcel.Integrity, originExcel.Availability, originExcel.Authentication,
		originExcel.CveStatus, originExcel.CreateTime, originExcel.UpdateTime).Exec()
	return err
}

func SelectCveOriginExcel(scoreType string, ORM orm.Ormer) ([]models.OriginExcel, int64, error) {
	var originExcel []models.OriginExcel
	sqlString := "SELECT cve_num,pack_name,cve_version FROM cve_origin_excel WHERE " +
		"score_type = ? OR score_type IS NULL OR cve_desc IS NULL OR nvd_score IS NULL"
	rowsAffected, err := ORM.Raw(sqlString, scoreType).QueryRows(&originExcel)
	return originExcel, rowsAffected, err
}

func SelectCveVulnCenter(ORM orm.Ormer) ([]models.VulnCenter, int64, error) {
	var vulnCenter []models.VulnCenter
	sqlString := "SELECT cve_id, cve_num, repair_time FROM cve_vuln_center WHERE repair_time = '' OR repair_time IS NULL"
	rowsAffected, err := ORM.Raw(sqlString).QueryRows(&vulnCenter)
	return vulnCenter, rowsAffected, err
}

func UpdateCveVulnCenter(repairTime, cveId string, ORM orm.Ormer) error {
	sqlString := "update cve_vuln_center set repair_time = ? where cve_id = ?"
	_, err := ORM.Raw(sqlString, repairTime, cveId).Exec()
	return err
}

func QueryCveByNumber(cveNum string, ORM orm.Ormer) ([]models.VulnCenter, int64, error) {
	var vulnCenter []models.VulnCenter
	sqlString := "select cve_num, pack_name, cve_version, cve_desc,repair_time," +
		"cve_status,cve_id from cve_vuln_center where cve_num = ? order by cve_id desc"
	rowsAffected, err := ORM.Raw(sqlString, cveNum).QueryRows(&vulnCenter)
	return vulnCenter, rowsAffected, err
}
